<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;


use app\admin\model\ShopModel;
use app\admin\model\ApplyModel;
use app\api\model\WxUserModel;
class Shop extends Base
{
    //用户列表
    public function index()
    {
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {
                $where['user_name'] = ['like', '%' . $param['searchText'] . '%'];
            }
            $user = new UserModel();
            $selectResult = $user->getUsersByWhere($where, $offset, $limit);

            $status = config('user_status');

            // 拼装参数
            foreach($selectResult as $key=>$vo){

                $selectResult[$key]['last_login_time'] = date('Y-m-d H:i:s', $vo['last_login_time']);
                $selectResult[$key]['status'] = $status[$vo['status']];

                if( 1 == $vo['id'] ){
                    $selectResult[$key]['operate'] = '';
                    continue;
                }

                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }

            $return['total'] = $user->getAllUsers($where);  //总数据
            $return['rows'] = $selectResult;

            return json($return);
        }

        return $this->fetch();
    }

    //航班订单列表
    public function shopApply(){
         if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
             $id = null;
             $where = [];
             if (!empty($param['searchText'])) {
//                $where['name'] = ['like', '%' . $param['searchText'] . '%'];
                $id = $param['searchText'];
                $where['id'] = $param['searchText'];
            }
            $shopApply = new ApplyModel();
            $selectResult = $shopApply->getApplyByWhere($id, $offset, $limit);
            $status = config('apply_status');

            // 拼装参数
            foreach($selectResult as $key=>$vo){

                $selectResult[$key]['create_time'] = date('Y-m-d H:i:s', $vo['create_time']);
                $selectResult[$key]['status'] = $status[$vo['status']];

                if( $vo['status']== 1){
                    $selectResult[$key]['operate'] = '';
                }else{
                    $selectResult[$key]['operate'] = showOperate($this->makeApplyButton($vo['id']));
                }
            }
            $return['total'] = $shopApply->getAllApply($where);  //总数据

            $return['rows'] = $selectResult;

            return json($return);
        }

        return $this->fetch();
    }

    //旅游订单列表
    public function shopProduct(){
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $id = null;
            $where = [];
            if (!empty($param['searchText'])) {
//                $where['name'] = ['like', '%' . $param['searchText'] . '%'];
                $id = $param['searchText'];
                $where['id'] = $param['searchText'];
            }
            $shopApply = new ApplyModel();
            $selectResult = $shopApply->getProductByWhere($id, $offset, $limit);
            $status = config('apply_status');
            $types = config('product_type');

            // 拼装参数
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['trips_time'] = date('Y-m-d H:i:s', $vo['trips_time']);
                $selectResult[$key]['status'] = $status[$vo['status']];
                if (isset($types[$vo['type']])) {
                    $selectResult[$key]['type'] = $types[$vo['type']];
                }
                if( $vo['status']== 1){
                    $selectResult[$key]['operate'] = '-';
                }else{
                    $selectResult[$key]['operate'] = showOperate($this->makeApplyButton($vo['id']));
                }
            }
            $return['total'] = $shopApply->getAllProduct($where);  //总数据
            $return['rows'] = $selectResult;
            return json($return);
        }

        return $this->fetch();
    }

    // 添加用户
    public function shopAdd()
    {
            $id = input('param.id');
           $shopApply = new ApplyModel();
            $shop = new ShopModel();
            $max_live_id= $shop->max('live_id');
           $apply=$shopApply->getOneApply($id);
          
           $data['shop_name']=$apply['shop_name'];
           $data['head_img']=$apply['door_img'];
           $data['address']=$apply['area'];
           $data['linkman']=$apply['name'];
           $data['mobile']=$apply['mobile'];
           $data['shop_type']=2;
           $data['add_time']=time();
           $data['auth_time']=time();
           $data['live_id']=$max_live_id+1;
            $wx_user=new WxUserModel();
            $where['mobile']=$data['mobile'];
        
            $user_info=$wx_user->getOneWxUser($where);
            $data['wx_user_id']=$user_info['id'];
            $flag = $shop->insertShop($data);
            if($flag['code']==1){
                
               $result= $shopApply->editApply(['status'=>1],['mobile' =>$apply['mobile']]);
             $res= $this->sendMsg($apply['mobile']);
            }
           
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        
    }

    // 编辑用户
    public function shopEdit()
    {
        $user = new UserModel();

        if(request()->isPost()){

            $param = input('post.');

            if(empty($param['password'])){
                unset($param['password']);
            }else{
                $param['password'] = md5($param['password']);
            }
            $flag = $user->editUser($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        $id = input('param.id');
        $role = new RoleModel();

        $this->assign([
            'user' => $user->getOneUser($id),
            'status' => config('user_status'),
            'role' => $role->getRole()
        ]);
        return $this->fetch();
    }

    // 修改状态
    public function shopDel()
    {
        $id = input('param.id');
        $role = new ApplyModel();
        $flag = $role->delApply($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    // 修改状态
    public function productDel()
    {
        $id = input('param.id');
        $role = new ApplyModel();
        $flag = $role->delProduct($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }
    //查看申请详情
    public function applyInfo(){
   
            $id = input('param.id');
           $shopApply = new ApplyModel();
           $apply=$shopApply->getOneApply($id);
            $this->assign('apply',$apply);
            return $this->fetch();
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'shop/useredit',
                'href' => url('shop/userEdit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'shop/userdel',
                'href' => "javascript:userDel(" .$id .")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }
     /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeApplyButton($id)
    {
        return [
//            '详情' => [
//                'auth' => 'shop/applyinfo',
//                'href' => url('shop/applyInfo', ['id' => $id]),
//                'btnStyle' => 'primary',
//                'icon' => 'fa fa-paste'
//            ],
            '审核' => [
                'auth' => 'shop/shopadd',
                'href' => "javascript:shopAdd(" .$id .")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-paste'
            ]
        ];
    }
    //发送短信
    private function sendMsg($mobile){
         //    短信
        $post_data = array();
        $post_data['account'] = "xumx_lz";
        $post_data['pswd'] = "Lz123456";
        $post_data['mobile'] =$mobile;
        $post_data['msg']= "【花凡】亲爱的花凡宝宝，恭喜您通过了花凡主播身份认证审核！赶紧打开花凡app，随时随地想播就播。"; 
        $post_data['needstatus'] ="true";
        $url='http://118.178.188.81/msg/HttpBatchSendSM';
        $o="";
        foreach ($post_data as $k=>$v)
        {
           $o.= "$k=".urlencode($v)."&";
        }
        $post_data=substr($o,0,-1);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $r= curl_exec($ch);
        return $r;
     }
}
